﻿Imports Entities
Imports DataAccess
Public Class GuiaBL
    Public Shared Function Save(ByVal zObject As GUIAS, ByVal zList As List(Of DETALLEGUIA)) As GUIAS
        Try
            Dim guia = GuiaDAL.Create(zObject)
            If IsNothing(guia) Then
                Return Nothing
            End If
            For Each rec In zList
                rec.GUIA = guia.GUIA
                DetalleGuiaDAL.Create(rec)
                ItemBL.UpdateStockMasivo(rec.ITEM, rec.CANTIDAD)
            Next
          
            Return guia
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Shared Function List() As List(Of GUIAS)
        Dim lista As New List(Of guias)
        lista = guiadal.List
        If lista.Count > 0 Then
            Return lista
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function SearchById(ByVal zId As Long) As GUIAS
        Dim bol As New guias
        bol = GuiaDAL.SearchById(zId)
        Return bol
    End Function

    Public Shared Function ListByFacturaId(ByVal zFactutaId As Long) As List(Of GUIAS)
        Dim lista As New List(Of GUIAS)
        lista = GuiaDAL.ListByFacturaId(zFactutaId)
        If lista.Count > 0 Then
            Return lista
        Else
            Return Nothing
        End If
    End Function
    Public Shared Function GetDetalleGuia(ByVal zGuiaId As Long) As List(Of DETALLEGUIA)
        Dim lista As New List(Of DETALLEGUIA)
        lista = DetalleGuiaDAL.ListByGuiaId(zGuiaId)
        If lista.Count > 0 Then
            Return lista
        Else
            Return Nothing
        End If
    End Function


    Public Shared Function GetDetalleGuiaP(ByVal zFactutaId As Long) As List(Of GuiaP)
        Dim listaGuias As New List(Of GUIAS)
        Dim listadetGuia As New List(Of DETALLEGUIA)
        Dim listafactura As New List(Of DETALLEFACTURA)
        Dim listaP As New List(Of GuiaP)
        Dim factura As FACTURAS = FacturaBL.SearchById(zFactutaId)
        listaGuias = GuiaDAL.ListByFacturaId(zFactutaId)
        listafactura = FacturaBL.GetDetalleFactura(zFactutaId)

        If listaGuias.Count = 0 Then
            If factura.SINGUIA Then Return Nothing
            For Each rec In listafactura
                Dim p As New Entities.GuiaP
                Dim item As Integer = rec.ITEM
                With p
                    .ItemId = rec.ITEM
                    .Descripcion = ItemBL.GetDescripcion(rec.ITEM)
                    .CantidadIngresada = 0
                    .CantidadGuia = 0
                    .CantidadPendiente = rec.CANTIDAD
                    .CantidadTotal = GetTotal(rec.ITEM, listafactura)
                    .PrecioUnitario = rec.PRECIOUNITARIO
                    .Total = 0
                End With
                listaP.Add(p)
            Next
        Else
            For Each fac In listafactura
                Dim cant As Integer = 0
                For Each guia In listaGuias
                    listadetGuia = GuiaBL.GetDetalleGuia(guia.GUIA)
                    For Each detalle In listadetGuia
                        If fac.ITEM = detalle.ITEM Then
                            cant += detalle.CANTIDAD
                        End If
                    Next
                Next
                Dim p As New Entities.GuiaP
                With p
                    .ItemId = fac.ITEM
                    .Descripcion = ItemBL.GetDescripcion(fac.ITEM)
                    .CantidadGuia = 0
                    .CantidadIngresada = cant
                    .CantidadPendiente = fac.CANTIDAD - cant
                    .CantidadTotal = GetTotal(fac.ITEM, listafactura)
                    .PrecioUnitario = fac.PRECIOUNITARIO
                    .Total = 0
                End With

                If p.CantidadPendiente > 0 Then
                    listaP.Add(p)
                End If

            Next
        End If
        If listaP.Count > 0 Then
            Return listaP
        Else
            Return Nothing
        End If
    End Function
    Private Shared Function GetTotal(ByVal ItemId As Long, ByVal lista As List(Of DETALLEFACTURA)) As Long
        Dim tot As Integer = 0
        For Each fila In lista
            If fila.ITEM = ItemId Then tot += fila.CANTIDAD
        Next
        Return tot
    End Function

End Class


